﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace NetworkSupervisor.Helper
{
    public static class HtmlHelper
    {
        /// <summary>
        /// 获取远程文件Html代码
        /// </summary>
        /// <param name="url"> </param>
        /// <param name="encoding"> </param>
        /// <returns></returns>
        public static string GetRemoteHtml(string url, Encoding encoding)
        {
            WebRequest req = WebRequest.Create(url);

            req.Method = "GET";
            try
            {
                using (WebResponse rep = req.GetResponse())
                {
                    using (Stream a = rep.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(a, encoding))
                        {
                            string html = sr.ReadToEnd();
                            return Regex.Replace(html, "[\r\n]", "");
                        }
                    }
                }
            }
            catch
            {
                return "";
            }
        }


        /// <summary>
        /// 移除Html标签
        /// </summary>
        /// <param name="htmlCode"></param>
        /// <returns></returns>
        public static string RemoveHtmlTag(string htmlCode)
        {
            return Regex.Replace(htmlCode, @"<.*?>|\s|&nbsp;", "", RegexOptions.IgnoreCase);
        }
    }
}